package com.lastcrdg.pieview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * author：liluo
 * e-mail：lastcrazysun@163.com
 * company：cusc
 * date：2017/9/29 9:43
 * description：1张素材截取对勾
 */

public class SinglePictureView extends View {

    private static final String TAG = SinglePictureView.class.getSimpleName();
    private Context mContext;
    public static final int CHECK_INFO = 0x001;
    public static final int UNCHECK_INFO = 0x002;
    private int width;
    private int height;
    private Paint mPaint = new Paint();
    private Paint drawPaint = new Paint();
    private Bitmap mBitmap;
    private Rect dst;
    private Rect checkSrc;
    private int page = 0;
    private int isCheck = 0; //默认0， check 1, uncheck -1;
    private int timeSum = 50;

    public void setTimeSum(int timeSum) {
        this.timeSum = timeSum;
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case CHECK_INFO:
                    if (page < 13 && page >= 0) {
                        setCheckRect(1, page);
                        invalidate();
                    }
                    break;
                case UNCHECK_INFO:
                    if (page == 0) {
                        isCheck = 0;
                        invalidate();
                    }
                    if (page > 0 && page < 13) {
                        setCheckRect(-1, page);
                        invalidate();
                    }
                    break;
            }
        }
    };

    public SinglePictureView(Context context) {
        this(context, null);
    }

    public SinglePictureView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SinglePictureView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initPaint();
        getBitmap();
    }

    private void initPaint() {
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.MAGENTA);
        mPaint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = MeasureSpec.getSize(widthMeasureSpec);
        height = MeasureSpec.getSize(heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.translate(width / 2, height / 2);
        canvas.drawCircle(0, 0, 200, mPaint);
        if (isCheck != 0) {
            canvas.drawBitmap(mBitmap, checkSrc, dst, drawPaint);
        }
        if (isCheck == 1 && page < 13) {
            page++;
            check(1);
        } else if (isCheck == -1 && page > 0) {
            page--;
            check(-1);
        }
    }


    private void getBitmap() {
        mBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.right);
    }

    private void check(int isCheck) {
        Message message = new Message();
        if (isCheck == 1) {
            message.what = CHECK_INFO;
        } else if (isCheck == -1) {
            message.what = UNCHECK_INFO;
        }
        mHandler.sendMessageDelayed(message, timeSum / 13);
    }

    private void setCheckRect(int isCheck, int i) {
        if (isCheck == 1) {
            checkSrc = new Rect(0, 0, mBitmap.getWidth() * (i + 1) / 13, mBitmap.getHeight());
            dst = new Rect(-150, -150, -150 + 300 * (i + 1) / 13, 150);
        } else if (isCheck == -1) {
            checkSrc = new Rect(0, 0, mBitmap.getWidth() * i / 13, mBitmap.getHeight());
            dst = new Rect(-150, -150, -150 + 300 * i / 13, 150);
        }
    }

    public void startCheck() {
        if (page == 0) {
            isCheck = 1;
            setCheckRect(1, page);
            invalidate();
        }
    }

    public void startUnCheck() {
        if (page == 13) {
            page = 12;
        }
        if (page == 12 && isCheck == 1) {
            isCheck = -1;
            setCheckRect(-1, page);
            invalidate();
        }
    }
}
